package com.itheima.reggie.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.UserHolder;
import com.itheima.reggie.domain.Cart;
import com.itheima.reggie.domain.Setmeal;
import com.itheima.reggie.mapper.CartMapper;
import com.itheima.reggie.service.CartService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class CartServiceImpl implements CartService {
    @Autowired
    private CartMapper cartMapper;
    //添加购物车
    @Override
    public Cart add(Cart cartParam) {
        //1. 查询当前菜品/套餐在数据表中是否存在(当前登录用户user_id) 调用userholder类查询到共享id
        //select * from cart where user_id = 登录用户id and dish_id = 传入的菜品id
        //select * from cart where user_id = 登录用户id and setmeal_id = 传入的套餐id
        LambdaQueryWrapper<Cart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Cart::getUserId, UserHolder.get().getId());
        wrapper.eq(cartParam.getDishId()!=null,Cart::getDishId,cartParam.getDishId());
        wrapper.eq(cartParam.getSetmealId()!=null,Cart::getSetmealId,cartParam.getSetmealId());
        Cart cart = cartMapper.selectOne(wrapper);
        //2. 查到了当前菜品/套餐在数据库的记录,  只需要将number+1 更新
        if (cart!=null){
            cart.setNumber(cart.getNumber() + 1);
            cartMapper.updateById(cart);
            return cart;
        }else {
            //3. 没有查到当前菜品/套餐的记录, 新增记录
            cartParam.setUserId(UserHolder.get().getId());//设置为登录用户id
            cartParam.setNumber(1);//设置为1
            cartParam.setCreateTime(new Date());//保存时间
            cartMapper.insert(cartParam);

            return cartParam;
        }
    }
    //查看购物车
    @Override
    public List<Cart> findAll() {
        //设置查询条件 根据我的用户id 匹配id
        //SELECT * FROM `shopping_cart` WHERE user_id = '1458310743471493121' ORDER BY create_time ASC
        LambdaQueryWrapper<Cart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Cart::getUserId,UserHolder.get().getId());
        //按照修改时间正序排列
        wrapper.orderByAsc(Cart::getCreateTime);
        List<Cart> cartList = cartMapper.selectList(wrapper);
        return cartList;
    }
    //清空购物车
    @Override
    public void delete() {
        //通过查找我的用户id 匹配id
        LambdaQueryWrapper<Cart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Cart::getUserId,UserHolder.get().getId());
        cartMapper.delete(wrapper);

    }
//修改购物车
    @Override
    public Cart update(Cart cartPram) {
        //1. 确定更新的购物车
        Long dishId = cartPram.getDishId();
        Long setmealId = cartPram.getSetmealId();

        LambdaQueryWrapper<Cart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(dishId != null, Cart::getDishId, dishId);
        wrapper.eq(setmealId != null, Cart::getSetmealId, cartPram.getSetmealId());
        Cart cart = cartMapper.selectOne(wrapper);

        //2. 执行更新/删除操作
        if (cart != null) {
            cart.setNumber(cart.getNumber() - 1);
            if (cart.getNumber() <= 0) {
                //删除
                cartMapper.deleteById(cart.getId());
            } else {
                //更新
                cartMapper.updateById(cart);
            }
        }
        return cart;
    }
}
